<template>
  <!------------------父组件----------------->
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="xunzhang" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- default-expand-all="true"默认展开 -->
        <el-tree
          :data="list"
          default-expand-all
        >
          <template v-slot="scope">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
              <el-col :span="20">
                <svg-icon icon-class="xunzhang" /><span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>{{ scope.data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(scope.data.id)">添加</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(scope.data.id)">编辑
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(scope.data.id)">删除</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 弹框出口 el-dialog中close-on-click-modal含义是：点击空白处是否关闭，默认true 关闭窗口这个动作，我们没法用确定的动作去判断这个值 在vue中统一加上了.sync来表示同步的修改了visible的值。-->
      <el-dialog :close-on-click-modal="false" :title="isEdit ? '编辑部门' : '添加部门'" :visible.sync="showDialog">
        <depDialog
          v-if="showDialog"
          ref="refDepDialog"
          :is-edit="isEdit"
          :pid="curId"
          :origin-list="originList"
          @success="hSuccess"
          @close="hClose"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
// --------------------------------------调用接口
import { getDepartments, delDepartmen } from '@/api/departments'
import { array2Tree } from '@/utils/index'
import depDialog from './depDialog.vue'
export default {
  components: { depDialog },
  // ------------------------------------数据
  data() {
    return {
      iconChange: true,
      originList: [],
      curId: '',
      showDialog: false,
      isEdit: false,
      list: []
    }
  },
  // ------------------------------------页面加载发送ajax
  created() {
    this.loadDepartments()
  },
  methods: {
    // ----------------------------------初始化渲染数据
    async loadDepartments() {
      try {
        // 保存数据
        const res = await getDepartments()
        // shift会取出数组第一个元素，同时会改变源数组
        const company = res.data.depts.shift()
        console.log('loadDepartments', company)
        // this.list = res.data.depts
        this.list = array2Tree(res.data.depts, '')
        // 后端返回的数据中 第一条是公司信息，它应该直接就是tree的根节点
        // 为了后面计算方便，把它直接取出来
        this.originList = res.data.depts.map(item => {
          return {
            id: item.id,
            pid: item.pid,
            name: item.name,
            code: item.code
          }
        })
      } catch (err) {
        console.log(err)
      }
    },
    // ----------------------------------用户点击添加(需要传入id)
    hAdd(id) {
      // 获取当前被点击的部门id
      console.log(id)
      this.curId = id
      this.showDialog = true
      // 3.添加
      this.isEdit = false
    },
    // ----------------------------------封装异步删除函数(需要传入id)
    async doDel(id) {
      // 1.调用接口做删除
      await delDepartmen(id)
      this.$message.success('删除成功')
      // 2.重发请求
      this.loadDepartments()
    },
    // ----------------------------------调用函数，删除部门(需要传入id)
    hDel(id) {
      // 确认框
      this.$confirm('是否要退出', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch(() => {

      })
    },
    // ----------------------------------用户点击编辑(需要传入id)
    hEdit(id) {
      // 显示弹框
      this.showDialog = true
      // 将单签编辑的部门id 传给子组件
      this.curId = id
      // 添加
      this.isEdit = true
      // 获取子组件的引用
      // vue数据修改导致视图更新。但是这个过程是异步的
      // $nextTick
      // 解决bug
      this.$nextTick(() => {
        console.log('获取子组件的引用', this.$refs.refDepDialog)
        this.$refs.refDepDialog.loadDepartmentById()
      })
      console.log(this.$refs.refDepDialog)
    },
    // ----------------------------------关闭弹框
    hClose() {
      this.showDialog = false
    },
    // ----------------------------------收到子组件的事件: 操作成功
    hSuccess() {
      // alert('收到子组件的事件: 操作成功')
      // 1. 关闭弹层
      this.showDialog = false
      // 2. 更新数据
      this.loadDepartments()
    }
  }
}

</script>
